﻿using System;
using System.Windows.Forms;
using FBA;
using FBAConfigurationManager.Properties;

namespace FBAConfigurationManager
{
	public partial class frmMain : Form
	{
		public frmMain()
		{
			InitializeComponent();
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				Settings.Default.Save();
				Cursor = Cursors.WaitCursor;
				ConfigureMembershipProvider.Configure(txtWebApplicationUrl.Text, cboZone.Text, txtSettingsXml.Text);
				MessageBox.Show("Web configs have been updated successfully.", "FBA Configuration Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
				Cursor = Cursors.Default;
			}
			catch (Exception ex)
			{
				Cursor = Cursors.Default;
				MessageBox.Show(ex.Message, "FBA Configuration Manager Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

		}

		private void frmMain_Load(object sender, EventArgs e)
		{
			Settings.Default.Reload();
			cboZone.SelectedIndex = 0;
			cboSampleConfiguration.Items.Add(new ConfigItem("eDirectory", Resources.eDirectory));
			cboSampleConfiguration.Items.Add(new ConfigItem("LDAP", Resources.LDAP));
			cboSampleConfiguration.Items.Add(new ConfigItem("OpenLDAP", Resources.OpenLDAP));
			cboSampleConfiguration.Items.Add(new ConfigItem("SqlMembership", Resources.SqlMembership));
			cboSampleConfiguration.Items.Add(new ConfigItem("SunJava", Resources.SunJava));

		}

		private void btnHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			var form = new frmHelp();
			form.ShowDialog();
		}

		private class ConfigItem
		{
			public ConfigItem(string name, string configuration)
			{
				Name = name;
				Configuration = configuration;
			}
			public string Name { get; set; }
			public string Configuration { get; set; }
		}

		private void cboSampleConfiguration_SelectedIndexChanged(object sender, EventArgs e)
		{
			txtSettingsXml.Text = ((ConfigItem)cboSampleConfiguration.SelectedItem).Configuration;
		}

		private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
		{
			Settings.Default.Save();
		}
	}
}
